package com.gitee.wsl.mathematics.matrix.mat2.ext

import com.gitee.wsl.mathematics.matrix.mat2.Matrix2
import com.gitee.wsl.mathematics.matrix.mat3.Matrix3
import com.gitee.wsl.mathematics.vector.vec2.Vector2

/**
 * Solve A * x = b where A = this matrix.
 *
 * @return The vector x that solves the above equation.
 */
fun <T:Number> Matrix2<T, *, *>.solve(b: Vector2<T, *>): Vector2<T, *> {
    val a11 = m00
    val a12 = m01
    val a21 = m10
    val a22 = m11
    var det = a11 * a22 - a12 * a21
    if (det != 0.0f) {
        det = 1.0f / det
    }
    val x = det * (a22 * b.x - a12 * b.y)
    val y = det * (a11 * b.y - a21 * b.x)
    return b.create(x,y) as Vector2<T, *>
}